<template>
    <PageLayout :show-search="showSearch" :beautiful="beautiful">
        <PageSearch
            ref="pageSearch"
            v-bind="$attrs"
            :before-rest-form="beforeRestForm"
            :pre-search="preSearchWhenLoading"
            @query-success="querySuccess"
        ></PageSearch>
        <!--1. 文字-->
        <ToolBar
            ref="toolBar"
            v-if="showToolBar"
            :tool-bar-frame="pageTableFrame"
            :show-search.sync="showSearch"
            :right-btn="beautiful"
        >
            <template v-for="(item, index) in buttons">
                <el-button
                    v-if="checkFunctionId(item.functionId)"
                    :key="index"
                    type="primary"
                    @click.native.prevent="item.click"
                    plain
                >
                    {{ item.value }}
                </el-button>
            </template>
        </ToolBar>
        <page-table
            ref="pageTable"
            :pagination="true"
            :total-count="totalCount"
            :page-table-frame="pageTableFrame"
            :page-table-data="tableData"
            :loading="loading"
            :row-key="$attrs.tableRowKey"
            :current-page="listPageQueryParam.pageNum"
            :page-size="listPageQueryParam.size"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        ></page-table>
    </PageLayout>
</template>
<script>
import { checkFunctionId } from '@/utils/permission.js'
import ToolBar from '@/components/ToolBar/index.vue'
import PageTable from './PageTable.vue'
import PageSearch from './PageSearch.vue'
import PageLayout from './PageLayout.vue'
export default {
    components: {
        ToolBar,
        PageTable,
        PageSearch,
        PageLayout
    },
    provide() {
        return {
            cellRenderSlotProvider: this
        }
    },
    props: {
        buttons: {
            type: Array,
            default: () => []
        },
        pageTableFrame: Array,
        preSearch: {
            type: Function,
            default: () => true
        },
        beautiful: {
            type: Boolean,
            default: true
        },
        showToolBar: {
            type: Boolean,
            default: true
        }
    },
    data() {
        return {
            totalCount: 0,
            showSearch: true,
            listPageQueryParam: { pageNum: 1, size: 10, start: 0 },
            tableData: [],
            loading: false
        }
    },
    created() {
        this.pageTableFrame.forEach((v) => {
            if (!v.hasOwnProperty('visible')) {
                this.$set(v, 'visible', true)
            }
        })
    },
    methods: {
        querySuccess(res) {
            this.tableData = res.data.items
            this.totalCount = res.data.totalItemNum
            this.loading = false
        },
        handleSizeChange(val) {
            this.listPageQueryParam.size = val
            this.calculateStart()
            this.onSearch()
        },
        handleCurrentChange(val) {
            this.listPageQueryParam.pageNum = val
            this.calculateStart()
            this.onSearch()
        },
        checkFunctionId,
        beforeRestForm() {
            this.listPageQueryParam.start = 0
            this.listPageQueryParam.pageNum = 1
        },
        calculateStart() {
            this.listPageQueryParam.start = (this.listPageQueryParam.pageNum - 1) * this.listPageQueryParam.size
        },
        preSearchWhenLoading(data) {
            data.start = this.listPageQueryParam.start
            data.size = this.listPageQueryParam.size
            let res = this.preSearch(data)
            if (res) {
                this.loading = true
            }
            return res
        },
        onSearch() {
            this.$refs.pageSearch.onSearch()
        }
    }
}
</script>
